<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover">
        <div class="user">
          <img :src="getImageUrl('user1')" />
          <div class="user-info">
            <p class="user-info-admin">Admin</p>
            <p class="user-info-p">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2025-02-03</span></p>
          <p>上次登录地点：<span>成都</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData">
          <el-table-column
            v-for="(val, key) in tableLabel"
            :key="key"
            :prop="key"
            :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 20px">
      <div class="num">
        <el-card
          :body-style="{ display: 'flex', padding: 0 }"
          v-for="item in countData"
          :key="item.name"
        >
          <component
            :is="item.icon"
            class="icons"
            :style="{ background: item.color }"
          ></component>
          <div class="detail">
            <span class="numSub">￥{{ item.value }}</span>
            <span class="text">￥{{ item.name }}</span>
          </div>
        </el-card>
      </div>

      <el-card class="top-echart">
        <div ref="echart" style="height: 280px"></div>
      </el-card>
      <div class="graph">
        <el-card> <div ref="userEchart" style="height: 300px"></div></el-card>
        <el-card> <div ref="videoEchart" style="height: 300px"></div></el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, getCurrentInstance, onMounted, reactive } from "vue";
import * as echarts from "echarts";
const { proxy } = getCurrentInstance();
const getImageUrl = (user) => {
  return new URL(`../assets/images/${user}.png`, import.meta.url).href;
};

const tableData = ref([]);
const countData = ref([]);
const chartData = ref([]);
const observer = ref(null);
const tableLabel = ref({
  name: "课程",
  todayBuy: "今日购买",
  monthBuy: "本月购买",
  totalBuy: "总购买",
});
const xOptions = reactive({
  textStyle: {
    color: "#333",
  },
  legend: {},
  grid: {
    left: "10%", // 减少左边距
    right: "10%", // 减少右边距
    top: "10%", // 减少上边距
    bottom: "10%", // 减少下边距
  },
  tooltip: { trigger: "axis" },
  xAxis: {
    type: "category",
    data: [],
    axisLine: { lineStyle: { color: "#17b3a3" } },
    axisLabel: {
      interval: 0,
      color: "#333",
    },
  },
  yAxis: [
    {
      type: "value",
      axisLine: {
        lineStyle: { color: "#17b3a3" },
      },
    },
  ],
  color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
  series: [],
});
const pieOptions = {
  // 全局文本样式
  textStyle: {
    color: "#333",
    fontSize: 12,
  },
  // 图例配置
  legend: {
    type: "scroll", // 当图例过多时可滚动
    orient: "vertical", // 垂直布局
    right: 10, // 距离右侧 10px
    top: "center", // 垂直居中
    textStyle: {
      color: "#666", // 图例文字颜色
    },
  },
  // 提示框配置
  tooltip: {
    trigger: "item",
    formatter: "{a} <br/>{b}: {c} ({d}%)", // 提示框显示格式，显示系列名、数据名、数值和百分比
  },
  // 系列配置
  series: [
    {
      name: "视频数据占比", // 系列名称
      type: "pie",
      radius: ["30%", "70%"], // 饼图的内外半径，形成环形饼图
      center: ["50%", "50%"], // 饼图的中心位置
      data: [],
      // 标签配置
      label: {
        show: true,
        formatter: "{b}: {d}%", // 标签显示格式，显示数据名和百分比
        position: "outside", // 标签位置在饼图外部
        color: "#666", // 标签文字颜色
      },
      // 标签线配置
      labelLine: {
        show: true,
        length: 10, // 标签线第一段长度
        length2: 20, // 标签线第二段长度
      },
      // 颜色配置
      color: ["#FF6B6B", "#6BFF6B", "#6B6BFF", "#FFD700", "#FF69B4", "#00BFFF"],
    },
  ],
};
const getTableData = async () => {
  const result = await proxy.$api.getTableData();
  tableData.value = result.tableData;
  // console.log(result);
};
const getCountData = async () => {
  const result = await proxy.$api.getCountData();
  countData.value = result;
  // console.log(result);
};
const getChartData = async () => {
  const { orderData, userData, videoData } = await proxy.$api.getChartData();

  //第一个图表x轴和series赋值
  xOptions.xAxis.data = orderData.date;
  xOptions.series = Object.keys(orderData.data[0]).map((val) => ({
    name: val,
    data: orderData.data.map((item) => item[val]),
    type: "line",
  }));
  const oneEchart = echarts.init(proxy.$refs["echart"]);
  oneEchart.setOption(xOptions);

  //对第二个表格进行渲染
  xOptions.xAxis.data = userData.map((item) => item.date);
  xOptions.series = [
    {
      name: "新增用户",
      data: userData.map((item) => item.new),
      type: "bar",
      // smooth: true,
    },
    {
      name: "活跃用户",
      data: userData.map((item) => item.active),
      type: "bar",
      // smooth: true,
    },
  ];
  const twoEchart = echarts.init(proxy.$refs["userEchart"]);
  twoEchart.setOption(xOptions);
  //对饼状图进行渲染
  pieOptions.series = [
    {
      data: videoData,
      type: "pie",
    },
  ];
  const threeEchart = echarts.init(proxy.$refs["videoEchart"]);
  threeEchart.setOption(pieOptions);

  observer.value = new ResizeObserver(() => {
    oneEchart.resize();
    twoEchart.resize();
    threeEchart.resize();
  });
  if (proxy.$refs["echart"]) {
    observer.value.observe(proxy.$refs["echart"]);
  }
};

onMounted(() => {
  getTableData();
  getCountData();
  getChartData();
});
</script>
<style scoped lang="less">
.home {
  height: 100%;
  // overflow: hidden;
  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    .user-info {
      p {
        line-height: 10px;
      }
      .user-info-admin {
        font-size: 35px;
      }
      .user-info-p {
        color: #999;
      }
    }
  }
  img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin-right: 40px;
  }
  .login-info {
    line-height: 30px;
    font-size: 14px;
    span {
      color: #666;
      margin-left: 60px;
    }
  }
  .user-table {
    margin-top: 20px;
  }
  .num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    // height: 250;
    .el-card {
      width: 30%;
      margin-bottom: 20px;
      // height: 130px;
    }
    .icons {
      width: 150px;
      height: 90px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #ffff;
    }
    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .numSub {
        font-size: 30px;
        margin-bottom: 10px;
        // height: 50%;
      }
      .text {
        font-size: 15px;
        text-align: center;
        color: #999;
        // height: 50%;
      }
    }
  }
  .graph {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
    .el-card {
      width: 48%;
      height: 300px;
    }
  }
}
</style>
